/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* Extra commands: remove the asterisk to install
*net install cleanplots, from("https://tdmize.github.io/data/cleanplots")
*set scheme cleanplots, perm

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Figures"
global figures = "$main\Figures"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------------------------------
* Temporal Dataset: broker shares
* -------------------------------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
gen double date=mdy(month_reg, day_reg, year_reg) // create a variable that contains the whole date
format date %td // set date format
drop if dow(date)==0 | dow(date)==6 // drop weekends

* Collapse
gen dec=1 // create constant for future collapse
collapse (sum) dec , by(key_dec date) // sum declarations by broker-date
encode key_dec , gen(broker) // create a categoric variable to balance the panel
xtset broker date // set dataset as a panel
tsfill , full // balance panel
drop if dow(date)==0 | dow(date)==6 // drop weekends
replace dec = 0 if dec==.

* New variables
gen week = week(date) // get week of the year
gen year = year(date) // get year
bys broker week year : egen week_decs = sum(dec) // weekly declarations
gen sharebro = dec/week_decs // daily share by inspector-date
label var sharebro "Broker share"

* Collapse
collapse (mean) sharebro , by(date) // average broker daily share of declarations by week
sort date 

* Save temporal dataset
save "$outputdata\Broker_shares.dta" , replace


* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset 

* Sample
gen double date=mdy(month_reg, day_reg, year_reg) // create a variable that contains the whole date
format date %td // set date format
drop if dow(date)==0 | dow(date)==6 // drop weekends

* Collapse
gen dec=1 // create constant for future collapse
collapse (sum) dec , by(insp_f date) // sum declarations by inspector-date
encode insp_f , gen(inspector) // create a categoric variable to balance the panel
xtset inspector date // set dataset as a panel
tsfill , full // balance panel
drop if dow(date)==0 | dow(date)==6 // drop weekends
replace dec = 0 if dec==.

* New variables
gen week = week(date) // get week of the year
gen year = year(date) // get year
gen month = month(date) // get month of the year
bys inspector week year : egen week_decs = sum(dec) // weekly declarations
gen shareinsp = dec/week_decs // daily share by inspector-date
replace shareinsp=0 if shareinsp==.
label var shareinsp "Inspector share"

* Dataset	
sort date // Sort observations
gen dow = dow(date) // create day of the week
label define dow 1 "Monday" 2 "Tuesday" 3 "Wednesday" 4 "Thursday" 5 "Friday"
label values dow dow
tab dow , g(day) // create dummies
local days `" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "'
forvalues i = 1/5 {
		local x: word `i' of `days'
		label var day`i' "`x'"
} // Loop: replace values of dow

* Merge	
merge m:1 date using "$outputdata\Broker_shares.dta", nogen
	
* Dataset
bys inspector week year : egen check = sum(shareinsp) // identify empty weeks
drop if check==0 // drop empty weeks
drop check

* New variables
gen share_diff = shareinsp-sharebro
label var share_diff "Inspector Share - Avg Broker Share"

* Replace values
for var sharebro shareinsp : replace X=X*100
replace share_diff = share_diff*100


* ---------
* Figure A1
* ---------

* Panel A
* -------
preserve
* Collapse
collapse (mean) shareinsp sharebro (sd) sdinsp=shareinsp sdbro=sharebro (count) ninsp=shareinsp nbro=sharebro , by(dow)

* New variables
gen hbro = sharebro + invttail(nbro-1,0.025)*(sdbro / sqrt(nbro))
gen lbro = sharebro - invttail(nbro-1,0.025)*(sdbro / sqrt(nbro))
gen hinsp = shareinsp + invttail(ninsp-1,0.025)*(sdinsp / sqrt(ninsp))
gen linsp = shareinsp - invttail(ninsp-1,0.025)*(sdinsp / sqrt(ninsp))

* Keep only necessary variables
keep dow* share* l* h*

* Reshape
reshape long share l h , i(dow) j(id) str
clonevar x = dow
replace x = cond(id=="insp" , x - 0.2 , x + 0.2)

* Figure
twoway (bar share x if id=="insp", fcol(gs10) lcol(gs10) lwidth(thin) barwidth(0.4)) /// 
	(bar share x if id=="bro" , fcol(gs6) lcol(gs6) lwidth(thin) barwidth(0.4))	///
	(rcap h l x, lcol(gs3) lwidth(thin)), ///
	xlabel(1(1)5 1 "Monday" 2 "Tuesday" 3 "Wednesday" 4 "Thursday" 5 "Friday", format(%5.0f) nogrid) ///
	ylabel(0(5)40, nogrid) ///
	ytitle("% of declarations registered", size(small) color(black)) ///
	xtitle("") ///
	legend(pos(6) ring(10) row(1) order(1 "Average inspector share" 2 "Average broker share")) ///
	name(Figure_A1a, replace)
graph export "$figures\Figure_A1a.eps", as(eps) replace
graph export "$figures\Figure_A1a.pdf", as(pdf) replace
restore

* Panel B
* -------
graph box shareinsp sharebro, over(dow) ///
	box(1, fcol(gs10) lcol(gs10) lwidth(thin)) ///
	box(2, fcol(gs6) lcol(gs6) lwidth(thin)) ///
	label noout ///
	ylabel(0(10)100, nogrid) ///
	ytitle("% of declarations registered", size(small) color(black)) ///
	legend(pos(6) ring(10) row(1) order(1 "Inspector share" 2 "Broker share")) ///
	note("")  ///
	name(Figure_A1b, replace)
graph export "$figures\Figure_A1b.eps", as(eps) replace
graph export "$figures\Figure_A1b.pdf", as(pdf) replace


* ----------------------
* Erase temporal dataset
* ----------------------
erase "$outputdata\Broker_shares.dta"


* -------------------------------- ENDS HERE -------------------------------- *